<template>
	<view class="page">
		<view class="bar_info" :style="{height:(88+StatusBarRpx)+'rpx',paddingTop:StatusBarRpx+'rpx'}">
			<view class="title_bar">
				<view class="title_bar_left">
					<image :src='STATIC_URL+"7.png"' mode="aspectFill"></image>
					<text>洛阳聚肴商贸有限公司</text>
				</view>
				<view class="title_bar_right"></view>
			</view>
		</view>
		<view :style="{height: (88+StatusBarRpx)+'rpx'}"></view>

		<view class="loading_css" v-if="isShow">
			<u-loading size="36" color="#41AF36"></u-loading>
			<view class="loading_text">加载中</view>
		</view>

		<block v-else>
			<view class="search_info" @tap.stop="handleJump" data-type="navigateTo" :data-url="`/pages/index/search`">
				<image class="search_icon" :src='STATIC_URL+"9.png"' mode="aspectFill"></image>
				<input class="search_input" type="text" placeholder="输入商品关键词"
					placeholder-style="font-size: 26rpx;color: #8B8B8B;" :disabled="true">
			</view>
			<view class="search_label">
				<!-- <view class="search_label_item" v-for="(v, k) in 5">牛奶</view> -->
			</view>
			<view class="classification" v-if="categoryList.length > 0">
				<view class="classification_list">
					<view class="classification_item" v-for="(v, k) in categoryList" :key="k"
						@tap.stop="selectCategory(v.id)">
						<image :src='v.image' mode="aspectFill"></image>
						<view class="classification_item_text">{{v.label}}</view>
					</view>
				</view>
			</view>
			<view class="tips_info flex align-center justify-center">
				<image :src='STATIC_URL+"17.png"' mode="aspectFill"></image>
				<text class="tips_text">商品</text>
				<image :src='STATIC_URL+"18.png"' mode="aspectFill"></image>
			</view>
			<view class="shop_list">
				<view class="shop_item flex" v-for="(item, index) in shopList" @tap.stop="handleJump"
					data-type="navigateTo" :data-url="`/pages/index/goodDetails?id=${item.id}`">
					<view class="item_left_img">
						<u-lazy-load :image="item.image" img-mode="aspectFill" height="180"
							border-radius="8"></u-lazy-load>
					</view>
					<view class="item_right">
						<view class="shop_name flex align-center justify-between">
							{{item.name}}
						</view>
						<view class="specification" v-if="item.specification">规格：{{item.specification}}</view>
						<view class="shop_price flex align-center justify-between">
							<view class="price_text">
								<template v-if="user.is_show_price == 1">
									<text class="price_1">￥</text>
									<text class="price_2">{{item.price}}</text>
									<text class="price_3">/{{filterDictionary(item.unit)}}</text>
								</template>
								<template v-else>
									<text class="price_3">单位：{{filterDictionary(item.unit)}}</text>
								</template>
							</view>
							<view class="shop_num flex align-center justify-end" @tap.stop="changeNum(item)">
								<template v-if="item.count > 0">
									<image :src='STATIC_URL+"19.png"' mode="aspectFill"></image>
									<view class="shop_num_input">{{item.count}}</view>
								</template>
								<image :src='STATIC_URL+"20.png"' mode="aspectFill"></image>
							</view>
						</view>
					</view>
				</view>
			</view>

			<view class="noData_other" v-if="shopList.length == 0 && !isShow">
				<image src="/static/empty/no.png"></image>
			</view>

		</block>
		<cart-popup :source="1" ref="cart" :item="shopItemInfo" @changeShop="changeShopInfo"></cart-popup>

	</view>
</template>

<script>
	import cartPopup from "@/components/cartPopup/cartPopup.vue"
	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				isShow: true,
				list: [],
				image: '/static/index/3.png',
				cardList: [], // 银行卡分类列表
				articleList: [], // 资讯列表
				card_index: [],

				cardIndex: 0,
				token: "",
				scene: "",
				categoryList: [],
				shopList: [],
				shopItemInfo: "",
				user: {},
				is_admin: 0, // 1有子账号  2无
				accountList: [], // 子账号列表
				show: false,
				customer_id: "", // 子账号id
				subAccount: "",
				selectAccountInfo: "",
			}
		},
		components: {
			cartPopup
		},
		computed: {
			// ...mapState(['cartList'])
		},
		watch: {
			// 监听vuex中的数据
			// cartList(newValue, oldValue) {
			// 	console.log(newValue);
			// 	let cartList = newValue;
			// 	if (cartList.length > 0) {
			// 		cartList.forEach((v, k) => {
			// 			this.shopList.forEach((v1, k1) => {
			// 				if (v.id == v1.id) {
			// 					v1.count = v.count
			// 				}
			// 			})
			// 		})
			// 	}
			// }
		},
		onLoad() {

		},
		onShow() {
			this.token = global.token;
			if (global.token) {
				this.getCategory();
				this.getDictionaryInfo();
				this.getUserInfo();

			} else {
				uni.reLaunch({
					url: '/pages/login/login'
				})
			}
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.isShow = true;
			this.getShopData();
		},
		onShareAppMessage(res) {
			if (res.from === 'button') { // 来自页面内分享按钮
				console.log(res.target)
			}
			return {
				title: '聚肴商贸',
				path: '/pages/tabbar/index'
			}
		},
		methods: {
			changeNum(item) {
				this.shopItemInfo = item;
				setTimeout(() => {
					this.$refs.cart.openCart();
				}, 100)
			},
			// 分类列表  glyCategory
			getCategory() {
				this.$api.get(global.apiUrls.glyCategory, {
					is_home: 1
				}).then(res => {
					// console.log(res);
					if (res.data.code == 200) {
						this.categoryList = res.data.data;
					} else {
						this.$message.info(res.data.message);
					}
				})
			},
			changeShopInfo(e) {
				let index = this.shopList.findIndex(item => item.id == e.id);
				this.shopList[index].count = e.count;
			},
			// 商品列表  gylHomeShop
			getShopData() {
				this.$api.get(global.apiUrls.gylHomeShop, {

				}).then(res => {
					// console.log(res);
					this.isShow = false;
					uni.stopPullDownRefresh();
					if (res.data.code == 200) {
						this.shopList = res.data.data;
						let cartList = this.$store.state.cartList;
						if (cartList.length > 0) {
							cartList.forEach((v, k) => {
								this.shopList.forEach((v1, k1) => {
									if (v.id == v1.id) {
										v1.count = v.count
									}
								})
							})
						}
					} else {
						this.$message.info(res.data.message);
					}
				})
			},
			getUserInfo() {
				this.$api.post(global.apiUrls.glyUser, {

				}).then(res => {
					// console.log(res);
					if (res.data.code == 200) {
						this.user = res.data.data;
						this.$store.commit('updateUserInfo', res.data.data);
					} else {
						this.$message.info(res.data.message);
					}
				})
			},

			// 数据字典  gylDictionary
			getDictionaryInfo() {
				this.$api.get(global.apiUrls.gylDictionary, {
					code: 'measurement_unit'
				}).then(res => {
					// console.log(res);
					if (res.data.code == 200) {
						global.dictionary = res.data.data;
						uni.setStorageSync('dictionary', res.data.data);
						this.getShopData();
					} else {
						this.$message.info(res.data.message);
					}
				})
			},
			selectCategory(category_id) {
				global.category_id = category_id;
				uni.switchTab({
					url: '/pages/tabbar/member'
				})
			},
		}
	}
</script>

<style lang="scss" scoped>

	.page {
		background: url(https://www.shiyaocanyin.com/images/8.png) no-repeat #f5f5f5;
		background-size: 100% auto !important;
		min-height: calc(100vh - var(--window-bottom));

		.bar_info {
			width: 750rpx;
			height: 88rpx;
			overflow: hidden;
			// border-bottom: 1px solid #eee;
			// background: #fff;
			z-index: 10;
			position: fixed;
			top: 0rpx;
			display: flex;
			background: url(https://www.shiyaocanyin.com/images/8.png) no-repeat;
			background-size: 100% auto;

			.title_bar {
				width: 100%;
				padding: 0 32rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.title_bar_left {
					display: flex;
					align-items: center;

					image {
						width: 36rpx;
						height: 36rpx;
					}

					text {
						font-weight: 500;
						font-size: 30rpx;
						color: #FFFFFF;
						margin-left: 10rpx;
					}
				}

				.title_bar_center {
					line-height: 88rpx;
					font-size: 36rpx;
					font-weight: 400;
					color: #fff;
				}

				.title_bar_right {
					
				}
			}
		}

		.search_info {
			margin: 10rpx 25rpx 0;
			width: 700rpx;
			height: 70rpx;
			background: #fff;
			border-radius: 100rpx;
			position: relative;

			.search_icon {
				width: 30rpx;
				height: 30rpx;
				position: absolute;
				left: 30rpx;
				top: 20rpx;
			}

			.search_input {
				height: 70rpx;
				display: flex;
				flex: 1;
				padding: 0 32rpx 0 80rpx;
				font-size: 26rpx;
			}
		}

		.search_label {
			overflow: hidden;
			margin: 15rpx 25rpx 0;

			.search_label_item {
				display: inline-block;
				padding: 1px 20rpx;
				background: rgba(255, 255, 255, 0.2);
				border-radius: 100rpx;
				font-weight: 400;
				font-size: 24rpx;
				color: #FFFFFF;
				margin: 0 20rpx 20rpx 0;
			}
		}

		.classification {
			margin: 10rpx 25rpx 0;
			background: #fff;
			border-radius: 10rpx;
			padding: 30rpx 0rpx 0;

			.classification_list {
				overflow: hidden;

				.classification_item {
					float: left;
					width: 25%;
					text-align: center;
					margin-bottom: 30rpx;

					image {
						width: 90rpx;
						height: 90rpx;
						border-radius: 50%;
					}

					.classification_item_text {
						font-weight: 400;
						font-size: 24rpx;
						color: #3D3D3D;
						margin-top: 10rpx;
						white-space: nowrap;
					}
				}
			}
		}

		.tips_info {
			padding: 30rpx 0;

			image {
				width: 108rpx;
				height: 5rpx;
			}

			.tips_text {
				font-weight: 500;
				font-size: 36rpx;
				color: #3D3D3D;
				margin: 0 20rpx;
			}
		}

		.shop_list {
			padding: 0 20rpx;

			.shop_item {
				background: #fff;
				border-radius: 10rpx;
				padding: 30rpx 20rpx;
				margin-bottom: 20rpx;

				.item_left_img {
					width: 180rpx;
					height: 180rpx;
				}

				/deep/.u-lazy-item {
					width: 180rpx !important;
					height: 180rpx !important;
				}

				.item_right {
					width: calc(100% - 180rpx);
					padding: 0 0 0 30rpx;

					.shop_name {
						font-weight: 500;
						font-size: 32rpx;
						color: #3D3D3D;
						text-overflow: ellipsis;
						overflow: hidden;
						white-space: nowrap;
					}

					.specification {
						margin-top: 16rpx;
						color: #999;
						font-size: 26rpx;
					}

					.shop_price {
						margin-top: 32rpx;

						.price_text {
							.price_1 {
								color: #D43C3B;
								font-size: 24rpx;
								font-weight: 600;
							}

							.price_2 {
								color: #D43C3B;
								font-size: 40rpx;
								font-weight: 600;
							}

							.price_3 {
								color: #989898;
								font-size: 24rpx;
								font-weight: 500;
							}
						}

						.shop_num {
							image {
								width: 60rpx;
								height: 40rpx;
							}

							.shop_num_input {
								text-align: center;
								margin: 0 16rpx;
								font-weight: 400;
								font-size: 30rpx;
								color: #3D3D3D;
							}
						}
					}
				}
			}
		}

		.swiper_box {
			padding: 38rpx 30rpx;
		}

		.swiper_nav {
			background: #fff;
			position: relative;

			swiper {
				height: 345rpx;
				// margin-bottom: 32rpx;
			}

			.swiper-item {
				display: flex;
				flex-wrap: wrap;
				justify-content: flex-start;
				align-items: center;

				.swiper-item_box {
					width: 25%;
					height: 130rpx;
					text-align: center;
					margin-bottom: 42rpx;

					image {
						width: 80rpx;
						height: 80rpx;
						margin: 0 auto;
					}

					.swiper-item_box_text {
						font-size: 26rpx;
						margin-top: 16rpx;
					}
				}
			}

			.swiper_dot {
				// position: absolute;
				// bottom: 0;
				// left: 50%;
				// transform: translateX(-50%);
				display: flex;
				justify-content: center;
				padding: 16rpx 0;

				.dot_item {
					display: inline-block;
					width: 12rpx;
					height: 12rpx;
					border-radius: 50%;
					background: #0278FC;
					opacity: 0.2;
					margin: 0 8rpx;
				}

				.active_dot_item {
					opacity: 1;
				}
			}
		}

	}
</style>